package thread;

/**
 * 常见的并发安全API
 * StringBuilder与StringBuffer

 * StringBuilder不是并发安全的，多线程场景下不能使用，此时应当换做使用
 * StringBuffer
 */
public class SyncDemo4 {
    public static void main(String[] args) {
//        StringBuilder sb = new StringBuilder();
        StringBuffer sb = new StringBuffer();
        //
        Thread t1 = new Thread(()->{
            for(int i=0;i<1000;i++){
                sb.append("abc");
            }
        });
        Thread t2 = new Thread(new Runnable(){
            @Override
            public void run() {
                for(int i=0;i<1000;i++){
                    sb.append("123");
                }
            }
        });

        t1.start();
        t2.start();
        // 需要主线程先等待子线程执行完毕
        try{
            Thread.sleep(50);
        }catch (Exception e){

        }
        System.out.println("字符串的长度为:" + sb.length());
        System.out.println("字符串的内容为:" + sb);

    }
}
